//
//  GJMeituContentView.m
//  CESHI
//
//  Created by Chen Qian on 2019/4/22.
//  Copyright © 2019 Chen Qian. All rights reserved.
//

#import "GJMeituContentView.h"
#import "meitu/GLMeituContentView.h"
#import "meitu/GLStoryboardSelectView.h"
#import "UIColorExted.h"

@interface GJMeituContentView ()<GLMeituContentViewDelegate,GLStoryboardSelectViewDelegate>

@property (nonatomic, strong) GLMeituContentView *meituContentView;

@end

@implementation GJMeituContentView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"图片合成";
    self.view.backgroundColor = [UIColor whiteColor];
    [self updateMetiuVie];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(void)toPreDispla{
    UIGraphicsBeginImageContextWithOptions(self.meituContentView.frame.size, NO, 2.0);
    
    [self.meituContentView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    if (_toPreDisplay) {
        _toPreDisplay(image);
    }
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)updateMetiuVie{
    
    CGRect statusRect = [[UIApplication sharedApplication] statusBarFrame];
    CGRect navRect = self.navigationController.navigationBar.frame;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGFloat barHeight = statusRect.size.height+navRect.size.height;
  
    BOOL iPhone_X = (((int)((screenSize.height/screenSize.width)*100) == 216)?YES:NO);
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"预览" style:UIBarButtonItemStylePlain target:self action:@selector(toPreDispla)];
    
  self.meituContentView = [[GLMeituContentView alloc] initWithFrame:CGRectMake(0, barHeight, screenSize.width, screenSize.height - barHeight - 60 - (iPhone_X ? 30 : 0))];
    _meituContentView.delegateMove = self;
    [self.view addSubview:_meituContentView];
    [_meituContentView setAssets:[NSMutableArray arrayWithArray:self.imageDatas]];
    [_meituContentView setStyleIndex:1];
    
    GLStoryboardSelectView *storyboardView = [[GLStoryboardSelectView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50 - (iPhone_X ? 30 : 0), self.view.frame.size.width, 50) picCount:[self.imageDatas count]];
    [storyboardView setBackgroundColor:[[UIColorExted colorWithHexString:@"#454545"] colorWithAlphaComponent:0.6]];
    storyboardView.delegateSelect = self;
    [self.view addSubview:storyboardView];
}

-(void)movedEditView{
    
}

- (void)didSelectedStoryboardPicCount:(NSInteger)picCount styleIndex:(NSInteger)styleIndex
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.meituContentView setStyleIndex:styleIndex];
    });
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)dealloc{
    _imageDatas = nil;
    _toPreDisplay = nil;
    _meituContentView = nil;
}

@end
